package cn.iocoder.yudao.module.vlog.history.dal.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.vlog.history.dal.dataobject.VLogBrowseHistoryDO;
import cn.iocoder.yudao.module.vlog.history.vo.VLogBrowseHistoryPageReqVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

/**
 * 视频浏览记录 Mapper
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Mapper
public interface VLogBrowseHistoryMapper extends CoreMapper<VLogBrowseHistoryDO> {

    default VLogBrowseHistoryDO selectOneByUserIdAndVlogId(Long userId, Long spuId) {
        CoreLambdaQueryWrapper<VLogBrowseHistoryDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(VLogBrowseHistoryDO::getUserId, userId);
        wrapper.eq(VLogBrowseHistoryDO::getVlogId, spuId);
        wrapper.limitN(1);
        wrapper.orderByDesc(VLogBrowseHistoryDO::getId);
        return selectOne(wrapper);
    }

    default PageResult<VLogBrowseHistoryDO> selectPage(VLogBrowseHistoryPageReqVO reqVO) {
        return selectPage(reqVO, new CoreLambdaQueryWrapper<VLogBrowseHistoryDO>()
                .eqIfPresent(VLogBrowseHistoryDO::getUserId, reqVO.getUserId())
                .eqIfPresent(VLogBrowseHistoryDO::getUserDeleted, reqVO.getUserDeleted())
                .eqIfPresent(VLogBrowseHistoryDO::getVlogId, reqVO.getVlogId())
                .betweenIfPresent(VLogBrowseHistoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(VLogBrowseHistoryDO::getId));
    }

    default void updateUserDeletedByUserId(Long userId, Collection<Long> spuIds, Boolean userDeleted) {
        update(new LambdaUpdateWrapper<VLogBrowseHistoryDO>()
                .eq(VLogBrowseHistoryDO::getUserId, userId)
                .in(CollUtil.isNotEmpty(spuIds), VLogBrowseHistoryDO::getVlogId, spuIds)
                .set(VLogBrowseHistoryDO::getUserDeleted, userDeleted));
    }

    default Page<VLogBrowseHistoryDO> selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) {
        Page<VLogBrowseHistoryDO> page = Page.of(pageNo, pageSize);
        return selectPage(page, new CoreLambdaQueryWrapper<VLogBrowseHistoryDO>()
                .eqIfPresent(VLogBrowseHistoryDO::getUserId, userId)
                .orderByAsc(VLogBrowseHistoryDO::getCreateTime));
    }

}
